package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.dao.PictureSheetOfMerchandiseDao;
import com.example.demo.pojo.CommodityList;
import com.example.demo.pojo.PictureSheetOfMerchandise;
import com.example.demo.service.ManageGoods;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ManageGoodsImpl implements ManageGoods {

    @Autowired
    CommodityListDao commodityListDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    PictureSheetOfMerchandiseDao pictureSheetOfMerchandiseDao;


    //商品上架
    @Override
    public int update(Integer id,Integer slaeStatu) {
        CommodityList commodityList=new CommodityList();
        commodityList.setSaleststus(id);
        commodityList.setGoodsid(id);
        return commodityListDao.updateByPrimaryKeySelective(commodityList);
    }



    //加入redies
    @Override
    public int addRedies(int id) {
        String ni="goodsid"+id;

        redisTemplate.opsForHash().putAll(ni,findGoodsAll(id));
        return 0;
    }

    //将商品从redies删除
    @Override
    public void deleteRediesGood(int id) {
        String goodsId="goodsid"+id;
        redisTemplate.opsForHash().getOperations().delete(goodsId);
    }

    //查询商品文字部分
    @Override
    public CommodityList findGoods(int goodsid) {
      return commodityListDao.selectByPrimaryKey(goodsid);
    }





  //查询商品主图加文字信息
    @Override
    public CommodityList findGoodsMaster(int goodid) {
        CommodityList commodityList=findGoods(goodid);
        PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
        pictureSheetOfMerchandise.setIss(1);
        pictureSheetOfMerchandise.setGoodsid(goodid);
        pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise).get(0).getPictureaddress();
        System.out.println( pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise).get(0).getPictureaddress());

        return commodityList;
    }


    //查询全部商品信息加所有展示图
    @Override
    public Map findGoodsAll(int goodsid) {
        Map map=new HashMap();
        map.put("first",JSONArray.toJSONString(findGoodsMaster(goodsid)));
        PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
        pictureSheetOfMerchandise.setGoodsid(goodsid);
        pictureSheetOfMerchandise.setIss(2);
        Iterator<PictureSheetOfMerchandise> iterator1=pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise).iterator();
        List<String>list=new ArrayList<>();
        while (iterator1.hasNext()){
            PictureSheetOfMerchandise pictureSheetOfMerchandise1=iterator1.next();
            list.add(pictureSheetOfMerchandise1.getPictureaddress());
        }
        System.out.println(list.toString());
        map.put("second",JSONArray.toJSONString(list));
        PictureSheetOfMerchandise pictureSheetOfMerchandise2=new PictureSheetOfMerchandise();
        pictureSheetOfMerchandise2.setGoodsid(goodsid);
        pictureSheetOfMerchandise2.setIss(3);
        Iterator<PictureSheetOfMerchandise> iterator2=pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise2).iterator();
        List<String>list1=new ArrayList<>();
        while (iterator2.hasNext()){
            PictureSheetOfMerchandise pictureSheetOfMerchandise1=iterator2.next();
            list1.add(pictureSheetOfMerchandise1.getPictureaddress());
        }
          map.put("third",JSONArray.toJSONString(list1));
        return  map;
    }


    //查询所有商品信息
    @Override
    public List<CommodityList> findAllByCommoditylist() {
       List<CommodityList> list=commodityListDao.findAllCommoditylist();
       for (int a=0;a<list.size();a++){
           int B=list.get(a).getGoodsid();
           PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
           pictureSheetOfMerchandise.setIss(1);
           pictureSheetOfMerchandise.setGoodsid(B);
           List<PictureSheetOfMerchandise> PI=pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise);
           list.get(a).setPicturepath(PI.get(0).getPictureaddress());
       }
       return list;
    }


}
